/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.reports;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pehulja.mmdo.coursework.math.SimplexTable;
import com.pehulja.mmdo.coursework.math.Solver;
import com.pehulja.mmdo.coursework.math.matrix.Matrix;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pehulja
 */
public class PdfReportCalculation {

    private static String FILE = "c:/FirstPdf.pdf";
    private static Font font;

    static {
        try {
            font = new Font(BaseFont.createFont("C:\\Windows\\Fonts\\ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
        } catch (DocumentException ex) {
            Logger.getLogger(PdfReportCalculation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PdfReportCalculation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private static void addContent(Document document, Solver solver) throws DocumentException {
        Anchor anchor = new Anchor("Условие задачи", font);
        anchor.setName("Условие задачи");
        anchor.setFont(font);
        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), 1);
        document.add(catPart);
        document.add(new Paragraph(solver.getTask().toString(), font));
        int k = 1;
        java.util.List<SimplexTable> simplexTables = solver.getSimplexTables();
        for (SimplexTable table : simplexTables) {
            addEmptyLine(document, 2);
            document.add(new Paragraph("Итерация" + k, font));
            addEmptyLine(document, 2);
            int n = table.getVectorC().length + 5;
            PdfPTable pdfTable = new PdfPTable(n);
            PdfPCell cell;
            for (int i = 0; i < 3; i++) {
                pdfTable.addCell(" ");
            }
            pdfTable.addCell("C");
            for (int i = 0; i < solver.getCanonicalLinearProgramTask().getGoalFunction().getVariablesCount(); i++) {
                pdfTable.addCell(Double.toString(solver.getCanonicalLinearProgramTask().getGoalFunction().getComponents().get(i).getFactor()));
            }
            pdfTable.addCell(" ");

            pdfTable.addCell("N");
            pdfTable.addCell("Cs");
            pdfTable.addCell("Fs");
            for (int i = 0; i < solver.getCanonicalLinearProgramTask().getGoalFunction().getVariablesCount() + 1; i++) {
                pdfTable.addCell("A" + Integer.toString(i));
            }
            pdfTable.addCell(new Phrase("Тета", font));
            int i;
            for (i = 0; i < table.getMatrixA().length; i++) {
                pdfTable.addCell(Integer.toString(i + 1));
                pdfTable.addCell(Double.toString(table.getVectorC()[table.getBasisComponents().get(i)]));
                pdfTable.addCell("A" + Integer.toString(table.getBasisComponents().get(i) + 1));
                for (int j = 0; j < table.getMatrixA()[i].length; j++) {
                    pdfTable.addCell(Double.toString(Matrix.round(table.getMatrixA()[i][j])));
                }
                if (table.getVectorQ() != null && table.getVectorQ().get(i) != null) {
                    pdfTable.addCell(Double.toString(Matrix.round(table.getVectorQ().get(i))));
                } else {
                    pdfTable.addCell(" ");
                }
            }
            if (table.getVectorDelta() != null) {
                for (i = 0; i < 2; i++) {
                    pdfTable.addCell(" ");
                }
                pdfTable.addCell(new Phrase("Дельта", font));
                for (i = 0; i < n - 4; i++) {
                    pdfTable.addCell(Double.toString(table.getVectorDelta()[i]));
                }
                pdfTable.addCell("");
            }
            document.add(pdfTable);
            k++;
        }
        if (solver.getTable().isOptimalPlanFinded()) {
            document.add(new Paragraph("Оптимальный базис задачи: ", font));
            document.add(new Paragraph("Оптимальный план задачи: ", font));
            document.add(new Paragraph("Значение целевой функции задачи: ", font));
        } else {
            document.add(new Paragraph("Целевая функция неограничена", font));
        }
    }

    private static void createTable(Section subCatPart) throws BadElementException {
        PdfPTable table = new PdfPTable(3);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Table Header 2"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Table Header 3"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        table.addCell("1.0");
        table.addCell("1.1");
        table.addCell("1.2");
        table.addCell("2.1");
        table.addCell("2.2");
        table.addCell("2.3");

        subCatPart.add(table);

    }

    private static void createList(Section subCatPart) {
        List list = new List(true, false, 10);
        list.add(new ListItem("First point"));
        list.add(new ListItem("Second point"));
        list.add(new ListItem("Third point"));
        subCatPart.add(list);
    }

    private static void addEmptyLine(Document document, int number) {
        for (int i = 0; i < number; i++) {
            try {
                document.add(new Paragraph(" "));
            } catch (DocumentException ex) {
                Logger.getLogger(PdfReportCalculation.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void createReport(Solver solver, String path) throws Exception {
        Document document = new Document(PageSize.A0, 10, 10, 10, 10);
        try {

            PdfWriter.getInstance(document, new FileOutputStream(path));
            document.open();
            /*      Anchor anchorTarget = new Anchor("First page of the document.");
             anchorTarget.setName("BackToTop");
             Paragraph paragraph1 = new Paragraph();

             paragraph1.setSpacingBefore(50);

             paragraph1.add(anchorTarget);
             document.add(paragraph1);
             document.add(new Paragraph("Some more text on the first page with different color and font type.",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 255, 0, 0))));
             */
            //addMetaData(document);
            addContent(document, solver);

        } catch (Exception ex) {
            throw ex;
        } finally {
            document.close();
        }
    }
}
